package com.tamtamy.jtta.utility;

import org.apache.commons.httpclient.methods.GetMethod;

import com.tamtamy.jtta.JTTAClient;
import com.tamtamy.jtta.conf.JTTAProperties;
import com.tamtamy.jtta.data.Metadata;
import com.tamtamy.jtta.data.Token;
import com.tamtamy.jtta.exception.UnauthorizedException;

/**
 * @author a.pellegatta
 *
 */
public class APIUtility {

	private JTTAClient jttaClient;
	private String baseUrl;
	private String type;
	private boolean debug;
	private Token token;
	private String appName = JTTAProperties.APP_NAME;
	
	public APIUtility(JTTAClient jttaClient, String baseUrl, String type, boolean debug) {
		this.jttaClient = jttaClient;
		this.baseUrl = baseUrl;
		this.type = type;
		this.debug = debug;
	}
	
	/**
	 * @param jttaClient
	 * @param url
	 * @param httpRequest
	 * @param debug
	 * @return
	 * @throws UnauthorizedException
	 */
	public String getResponse(GetMethod httpRequest, String httpURL) throws UnauthorizedException {
		String response = null;
		try {
			jttaClient.executeMethod(httpRequest);
			if(debug){
				System.out.println(	"HTTP Request : " + httpURL); 
									
			}
			if (httpRequest.getStatusCode()!=200){
				throw new UnauthorizedException("code " + httpRequest.getStatusCode());
			}
			response = httpRequest.getResponseBodyAsString().trim();
			if(debug){
				System.out.println("HTTP Response : " +	httpRequest.getStatusCode() + " - " + httpRequest.getStatusText() + "\n" + 
									response);
			}
		} catch (Exception exception) {
			System.out.println("Error calling " + httpURL + " : " + exception.getLocalizedMessage());
			throw new UnauthorizedException("Error calling " + httpURL + " : " + exception.getLocalizedMessage());
		}				
		httpRequest.releaseConnection();
		return response;
	}
	
	public String getAppNameParameter(){
		return "&appName=" + appName;
	}

	public String getTokenParameter(){
		return "?token=" + token.getToken();
	}
	
	public String getLoginURL(String username, String password) {
		return baseUrl + "/login" + type + "?userid=" + username + "&password=" + password + getAppNameParameter();
	}
	
	public String getLogoutURL(){
		return baseUrl + "/logout" + type + getTokenParameter() + getAppNameParameter();
	}

	public String getDownloadContentURL(String contentID) {
		return baseUrl + "/contentDownload/" +contentID + getTokenParameter() + getAppNameParameter();
	}
	
	public String getCommentURL(long commentID) {
		return baseUrl + "/comment/" + commentID + type + getTokenParameter() + getAppNameParameter();
	}
 
	public String getCommentListURL(long startOffset, long limit) {
		String commentListURL = this.baseUrl + "/comment" + this.type +  getTokenParameter();
		if(startOffset>-1){
			commentListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		commentListURL += getAppNameParameter();
		return commentListURL;
	}
	
	public String getContentFileURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentLinkURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentListURL(long startOffset, long limit, String filter, String order) {
		String contentURL = baseUrl + "/content" + type + getTokenParameter();	
		contentURL += "&startOffset=" + startOffset + "&limit=" + limit;		
		if(filter!=null){
			contentURL += "&filter="+order;
		}
		if(order!=null){
			contentURL += "&order="+filter;
		}
		contentURL += getAppNameParameter();
		return contentURL;
	}
	
	public String getContentWidgetURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}

	public String getGroupDetailsURL(String groupID) {
		return baseUrl + "/group/" + groupID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getGroupListURL(long startOffset, long limit) {
		String groupListURL = baseUrl + "/group" + type + getTokenParameter();	
		if(startOffset>-1){
			groupListURL += "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		groupListURL += getAppNameParameter();
		return groupListURL;
	}
	
	public String getMetadataURL(String url) throws Exception {
		String metadataURL = url + "/metadataGuess" + type + getTokenParameter();	
		metadataURL += "&url=" + Metadata.encodeURL(url) + getAppNameParameter(); 
		return metadataURL;
	}

	public String getNetworkDetailsURL(String networkID) {
		return baseUrl + "/network/" + networkID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentMessageURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getNetworkListURL(long startOffset, long limit) {
		String networkURL = baseUrl + "/network" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}

	public String getNetworkListURL(long startOffset, long limit, String filter) {
		String networkURL = baseUrl + "/network" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit + "&filter=" + filter;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}
	
	public String getNetworkListWithChatEnabledURL(long startOffset, long limit) {
		String networkURL = baseUrl + "/network-chat" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}

	public String getNetworkListWithChatEnabledURL(long startOffset, long limit, String filter) {
		String networkURL = baseUrl + "/network-chat" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit + "&filter=" + filter;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}
	
	public String getNoticeURL(long noticeID) {
		return baseUrl + "/notice/" + noticeID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getNoticeListURL(long startOffset, long limit, String filter) {
		String noticeListURL = baseUrl + "/notice" + type + getTokenParameter();
		if(startOffset>-1){
			noticeListURL +=  "&startOffset=" + startOffset + "&limit=" + limit;
		}
		if(filter!=null){
			noticeListURL += "&filter=" + filter;
		}
		noticeListURL += getAppNameParameter();
		return noticeListURL;
	}
	
	public String getOnlineUsersList(long startOffset, long limit) {
		String userListURL = baseUrl + "/on-line-user" + type + getTokenParameter();
		if(startOffset>-1){
			userListURL += "&startOffset=" + startOffset + "&limit=" + limit; 
		}
		userListURL += getAppNameParameter();
		return userListURL;
	}
	
	public String getTagListDetailURL(String tag) {
		return baseUrl + "/tag/" + tag + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getTagListURL(long startOffset, long limit) {
		String tagListURL = baseUrl + "/tag" + type + getTokenParameter();
		if(startOffset>-1){
			tagListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		tagListURL += getAppNameParameter();
		return tagListURL;
	}
	
	public String getUserDetailURL(String userID) {
		return baseUrl + "/user/" + userID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getUserStatsGenerationDetailsURL(String userStatsID,	long startOffset, long limit) {
		String userStatsGenerationDetailsURL = baseUrl + "/userStatsGeneration/" + userStatsID + type + getTokenParameter();
		if(startOffset>-1){
			userStatsGenerationDetailsURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}	
		userStatsGenerationDetailsURL += getAppNameParameter();
		return userStatsGenerationDetailsURL;
	}
	
	public String getUserStatsGenerationListURL(long startOffset, long limit) {
		String userStatsGenerationListURL = baseUrl + "/user-stats-generation" + type + getTokenParameter();
		if(startOffset>-1){
			userStatsGenerationListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		userStatsGenerationListURL += getAppNameParameter();
		return userStatsGenerationListURL;
	}

	public String getUserStatsURL(String userID, long startOffset, long limit) {
		String userStatsURL = baseUrl + "/userStats/" + userID + type + getTokenParameter();
		if(startOffset>-1){
			userStatsURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		userStatsURL += getAppNameParameter();
		return userStatsURL;
	}

	public String getUserTagURL(String filter) {
		String userTagURL = baseUrl + "/userTag" + type + getTokenParameter();
		if(filter!=null){
		  userTagURL += "&filter=" + filter;
		}
		userTagURL += getAppNameParameter();
		return userTagURL;
	}
	
	public String getUserTagURL(String userID, String filter) {
		String userTagURL = baseUrl + "/userTag/" + userID + type + getTokenParameter();
		if(filter!=null){
		  userTagURL += "&filter=" + filter;
		}
		userTagURL += getAppNameParameter();
		return userTagURL;
	}
	
	public String getUserListURL(long startOffset, long limit, String filter) {
		String userListURL = baseUrl + "/user" + type + getTokenParameter();
		if(startOffset>-1){
			userListURL += "&startOffset=" + startOffset + "&limit="+limit;
		}
		if(filter!=null){
			userListURL += "&filter=" + filter;
		}
		userListURL += getAppNameParameter();
		return userListURL;
	}
	
	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		this.token = token;
	}
	
	
}
